package cloud.cloud_web_ui.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import cloud.cloud_web_ui.model.User;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class UserService {

	@Autowired
	RestTemplate restTemplate;
	final String SERVICE_NAME = "cloud-web-service";

	@HystrixCommand(fallbackMethod = "fallbackSearchAll")
	public List<User> searchAll() {
		return restTemplate.getForObject("http://" + SERVICE_NAME + "/t1",
				List.class);
	}
	
	public String t1() {
		return restTemplate.getForObject("http://" + SERVICE_NAME + "/t1",
				String.class);
	}


	private List<User> fallbackSearchAll() {
		// System.out.println("HystrixCommand fallbackMethod handle!");
		List<User> ls = new ArrayList<User>();
		User user = new User();
		user.setUsername("TestHystrix");
		ls.add(user);
		return ls; 
	}
}